/**
 * @(#) NXT.java
 */

package pcsr.drivers.nxtDriver;

import pcsr.drivers.nxtDriver.NXTActuatorFactory.NXTActuatorFactoryType;
import pcsr.drivers.nxtDriver.NXTSensorFactory.NXTSensorFactoryType;
import pcsr.drivers.nxtDriver.command.DeviceInfo;
import pcsr.drivers.nxtDriver.command.INXTCommand;
import pcsr.drivers.nxtDriver.command.NXTCommand;

class NXT
{	
		
	private static INXTCommand nxtCommand = NXTCommand.GetSingleton();
	private NXTActuatorFactory _actuatorfactory = new NXTActuatorFactory();
	private NXTSensorFactory _sensorfactory = new NXTSensorFactory();
	
	private INXTActuator a_A;
	private INXTActuator a_B;
	private INXTActuator a_C;
	
	private INXTSensor s_1;
	private INXTSensor s_2;
	private INXTSensor s_3;
	private INXTSensor s_4;
	
	public void OpenConnection( )
	{
		OpenConnection(false);
	}
	
	public void OpenConnection( boolean verify )
	{
		nxtCommand.Open();
		nxtCommand.SetVerify(verify);
	}
	
	public void CloseConnection( )
	{
		nxtCommand.Close();
	}
	
	public void Initialize( boolean verify )
	{
		OpenConnection(verify);
		
		INXTActuator aa = GetActuatorFactory().createActuator(NXTPort.Actuator_A, NXTActuatorFactoryType.LightActuator); 
		SetActuator_A(aa);		
		INXTActuator ab = GetActuatorFactory().createActuator(NXTPort.Actuator_B, NXTActuatorFactoryType.MotorActuator);
		SetActuator_B(ab);
		INXTActuator ac = GetActuatorFactory().createActuator(NXTPort.Actuator_C, NXTActuatorFactoryType.MotorActuator);	
		SetActuator_C(ac);
		
		INXTSensor s1 = GetSensorFactory().createSensor(NXTPort.Sensor_1, NXTSensorFactoryType.LightSensor);
		SetSensor_1(s1);		
		INXTSensor s2 = GetSensorFactory().createSensor(NXTPort.Sensor_2, NXTSensorFactoryType.TiltSensor);
		SetSensor_2(s2);
		INXTSensor s3 = GetSensorFactory().createSensor(NXTPort.Sensor_3, NXTSensorFactoryType.TouchSensor);
		SetSensor_3(s3);
		INXTSensor s4 = GetSensorFactory().createSensor(NXTPort.Sensor_4, NXTSensorFactoryType.LightSensor);
		SetSensor_4(s4);

	}
	static String GetBrickName( ) {
		DeviceInfo i = nxtCommand.GetDeviceInfo();
		return i.GetNXTname();
	}
	
	/**
	 * This doesn't seem to be implemented in Lego NXT firmware/protocol?
	 * @return Seems to return 0 every time
	 */
	static int GetSignalStrength( ) {
		DeviceInfo i = nxtCommand.GetDeviceInfo();
		return i.GetSignalStrength();
	}
	
	void SetActuator_A( INXTActuator a_A ) {
		this.a_A = a_A;
	}

	INXTActuator GetActuator_A( ) {
		return a_A;
	}

	void SetActuator_B( INXTActuator a_B ) {
		this.a_B = a_B;
	}
	INXTActuator GetActuator_B( ) {
		return a_B;
	}

	void SetActuator_C( INXTActuator a_C ) {
		this.a_C = a_C;
	}
	INXTActuator GetActuator_C( ) {
		return a_C;
	}

	void SetSensor_1( INXTSensor s_1 ) {
		this.s_1 = s_1;
	}
	INXTSensor GetSensor_1( ) {
		return s_1;
	}

	void SetSensor_2( INXTSensor s_2 ) {
		this.s_2 = s_2;
	}
	INXTSensor GetSensor_2( ) {
		return s_2;
	}

	void SetSensor_3( INXTSensor s_3 ) {
		this.s_3 = s_3;
	}
	INXTSensor GetSensor_3( ) {
		return s_3;
	}

	void SetSensor_4( INXTSensor s_4 ) {
		this.s_4 = s_4;
	}
	INXTSensor GetSensor_4( ) {
		return s_4;
	}

	/**
	 * @return the _actuatorfactory
	 */
	NXTActuatorFactory GetActuatorFactory( ) {
		return _actuatorfactory;
	}
	
	/**
	 * @return the _sensorfactory
	 */
	NXTSensorFactory GetSensorFactory( ) {
		return _sensorfactory;
	}
}
